package br.com.site.model.entity;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.RespostaDAO;

/**
 * @author Bruno
 * 
 * TODO
 */
@Entity
public class Resposta implements CrudBaseEntity<Resposta, Long>, Auditoria {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;

	@OneToOne(targetEntity = br.com.site.model.entity.Pergunta.class, fetch = FetchType.LAZY)
	private Pergunta pergunta;

	@ManyToOne(targetEntity = br.com.site.model.entity.Usuario.class, fetch = FetchType.LAZY)
	private Usuario donoResposta;
	@Column(columnDefinition="text")
	private String texto = "";

	/* Auditoria */
	@Column(name = "loginUsuario", nullable = false)
	private String loginUsuario;

	@Column(name = "dtCriacao", nullable = false)
	private Date dtCriacao;

	@Column(name = "dtModificacao", nullable = false)
	private Date dtModificacao;

	@Column(name = "ativo", nullable = false)
	private boolean ativo;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLoginUsuario() {
		return loginUsuario;
	}

	public void setLoginUsuario(String loginUsuario) {
		this.loginUsuario = loginUsuario;
	}

	public Date getDtCriacao() {
		return dtCriacao;
	}

	public void setDtCriacao(Date dtCriacao) {
		this.dtCriacao = dtCriacao;
	}

	public Date getDtModificacao() {
		return dtModificacao;
	}

	public void setDtModificacao(Date dtModificacao) {
		this.dtModificacao = dtModificacao;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Pergunta getPergunta() {
		return pergunta;
	}

	public void setPergunta(Pergunta pergunta) {
		this.pergunta = pergunta;
	}

	public Usuario getDonoResposta() {
		return donoResposta;
	}

	public void setDonoResposta(Usuario donoResposta) {
		this.donoResposta = donoResposta;
	}


	public String getTexto() {
		return texto;
	}

	public void setTexto(String texto) {
		this.texto = texto;
	}

	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			RespostaDAO dao = factory.getDao(RespostaDAO.class);
			dao.remove(this.id);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}

	}

	@Override
	public void save() throws BusinessException {

		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			RespostaDAO dao = factory.getDao(RespostaDAO.class);
			dao.save(this);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}
	}

	public static Resposta find(Long oid) {
		DAOFactory factory = DAOFactory.getInstance();
		RespostaDAO dao = factory.getDao(RespostaDAO.class);

		return dao.find(oid);
	}

	public static List<Resposta> find() {
		DAOFactory factory = DAOFactory.getInstance();
		RespostaDAO dao = factory.getDao(RespostaDAO.class);

		return dao.find();
	}

	public static List<Resposta> findByCriteria(Resposta avisoMural) {
		DAOFactory factory = DAOFactory.getInstance();
		RespostaDAO dao = factory.getDao(RespostaDAO.class);

		return dao.findByCriteria(avisoMural);
	}

	public static List<Resposta> find(int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		RespostaDAO dao = factory.getDao(RespostaDAO.class);

		return dao.find(first, max);
	}

	public static List<Resposta> findByCriteria(Resposta avisoMural, int first,
			int max) {
		DAOFactory factory = DAOFactory.getInstance();
		RespostaDAO dao = factory.getDao(RespostaDAO.class);

		return dao.findByCriteria(avisoMural, first, max);

	}
}
